import Shop.damain.Users;
import Shop.mappers.UsersMapper;
import Shop.service.UserService;
import Shop.tools.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by BadHuge on 2020/10/2.
 */
public class Login extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        SqlSession sqlSession = MybatisUtil.getSqlSession();

        UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
        Users user2=new Users(username,password,0,"11",1,"11");
        Users user1 = usersMapper.login(user2);
                  /*  users = userService.login(name1,pwd1);*/
        if (user1 != null){
            System.out.print("登陆成功");
        HttpSession session = request.getSession();
        session.setAttribute("user",user1);
        response.sendRedirect("ShowGoods.do");
                         }
        else {
            response.sendRedirect("login.jsp");
            //System.out.print("登陆成功" + user1.getUsername());
        }
/*        UserService us = new UserService();
        Users users =  us.login(username,password);
        if (users!=null)
        {
            HttpSession session = request.getSession();
            session.setAttribute("user",users);
            response.sendRedirect("ShowGoods.do");
        }else
            response.sendRedirect("login.jsp");*/
    }
}
